package com.adong.juc;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 集合类是不安全
 */
public class NotSafeDemo {
    public static void main(String[] args) {
        List<String> list = new CopyOnWriteArrayList<>(); // 线程安全使用JUC的CopyOnWriteArrayList
        //Collections.synchronizedList(new ArrayList<>()); 线程安全,效率太低
        //new Vector<>(); JDK1.0 线程安全,但是太久远了
        //new ArrayList<>(); 不安全

        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(Thread.currentThread().getName() + list);
            }, String.valueOf(i)).start();
        }
    }
}
